package gr.atc.epart;

import gr.atc.epart.actions.ActionComment;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CommentsAdapter extends BaseAdapter {

	private Context context;
	private List<ActionComment> commentList;

	int page = 0;

	public CommentsAdapter(Context context, List<ActionComment> commentList) {

		this.context = context;
		this.commentList = commentList;

	}

	public int getCount() {

		return commentList.size();

	}

	public void update() {

		// weatherList.clear();
		// TODO:: do the update....

		this.notifyDataSetChanged();
	}

	public Object getItem(int position) {
		return commentList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		// int last = (getCount()-1);
		//	
		//		
		// if (position == last) {
		//				
		// update();
		//				
		// //return new MoreView(context);
		//			
		// } else {
		//			
		// Weather weather = weatherList.get(position);
		// return new WeatherAdapterView(this.context, weather);
		//		    
		// }

		int last = (getCount() - 1);

		if (position == last) {

			//Toast.makeText(context, "Loading...", Toast.LENGTH_SHORT).show();

			page = page + 1;
			update();
			// position = 0;

		}

		ActionComment comment = commentList.get(position);

		View v = convertView;

		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.commentrow, null);
		}

		if (comment != null) {
			TextView commentOwner = (TextView) v
					.findViewById(R.id.commentowner);
			TextView commentDate = (TextView) v.findViewById(R.id.commentdate);
			TextView commentText = (TextView) v.findViewById(R.id.commenttext);

			commentOwner.setText(comment.getUserName());
			// TODO parse date... now it's fixed
			commentDate.setText("11-11-09");
			commentText.setText(comment.getComment());

		}

		return v;
	}
}